<?php

namespace App\Http\Controllers\Api\V1;

use App\Enums\ResponseCode;
use App\Exceptions\ApiExceptions;
use App\Exceptions\BusinessException;
use App\Http\Controllers\Api\AbstractApiController;
use App\Services\Pack\PackService;
use App\Services\Shop\ProductLogic;
use App\Services\Warehouse\WarehouseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class WarehouseController extends AbstractApiController
{
    /**
     * 场次列表
     * @return JsonResponse
     */
    public function index(): JsonResponse
    {
        $user = auth('api')->user();
        $result = WarehouseService::list($user);
        return $this->success(trans("success"), ResponseCode::OK, compact('result'));
    }

    /**
     * 拼团商品列表
     * @param Request $request
     * @return JsonResponse
     */
    public function productList(): JsonResponse
    {
        $product = (new ProductLogic())->list(null,1);
        $max_price  =  WarehouseService::max_price(auth('api')->user());
        return $this->success(trans("success"), ResponseCode::OK, compact('product','max_price'));
    }
    /**
     * 参与拼团
     * @param Request $request
     * @return JsonResponse
     * @throws ApiExceptions
     * @throws BusinessException
     */
    public function purchase(Request $request): JsonResponse
    {
        $input = $request->validate([
            'product' => 'required|array',
            'address_id' => 'required|integer|min:1',
        ]);
        //检查安全密码
        WarehouseService::purchase(auth('api')->user(), $input['product'], $input['address_id']);
        return $this->success(trans("success"),);
    }


}
